package com.lzq.springbook.service;

import com.lzq.springbook.enums.BookStatus;
import com.lzq.springbook.mapper.BookInfoMapper;
import com.lzq.springbook.model.BookInfo;
import com.lzq.springbook.model.PageRequest;
import com.lzq.springbook.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class BookService {
    @Autowired
    private BookInfoMapper bookInfoMapper;
    public void addBook(BookInfo bookInfo) {
        bookInfoMapper.insertBook(bookInfo);
    }

    public PageResult<BookInfo> getBookListByPage(PageRequest pageRequest) {
        Integer count = bookInfoMapper.count();
        List<BookInfo> books = bookInfoMapper.queryBookListByPage(pageRequest.getOffset(),pageRequest.getPageSize());
        for(BookInfo book: books) {
            book.setStateCN(BookStatus.getNameByCode(book.getStatus()).getName());
        }
        return new PageResult<>(count,books,pageRequest);
    }

    public BookInfo getBookInfo(Integer bookId) {
        return bookInfoMapper.getBookInfo(bookId);
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookInfoMapper.updateBook(bookInfo);
    }

    public Boolean batchDelete(List<Integer> list) {
        return bookInfoMapper.batchDelete(list);
    }
}
